import ccclass = cc._decorator.ccclass;
import PopupWindow from "../../framework/PopupWindow";
import property = cc._decorator.property;
import { WindowName } from "../../framework/reflect/WindowName";
import Context from "../../framework/Context";
import CommonUtil from "../../framework/utils/CommonUtil";
import { WxEvent } from "../../framework/wx/WxEvent";
import LocalStorage from "../../framework/utils/LocalStorage";
import LoadResData from "../../framework/LoadResData";

@ccclass
@WindowName("TalentPopup")
export default class TalentPopup extends PopupWindow {
    @property(cc.Label)
    private text1: cc.Label = null;
    @property(cc.Label)
    private text2: cc.Label = null;
    @property(cc.Node)
    private returnBtn: cc.Node = null;
    @property(cc.Button)
    private activateBtn: cc.Button = null;
    private state = 0;
    private id = 0;
    public static showTalent(data): Promise<TalentPopup> {
        return Context.get().popup(TalentPopup, false).then(talent => {
            talent.initData(data);
            return Promise.resolve(talent);
        })
    }
    public initData(data) {
        this.id = data['id'];
        this.state = data['state'];
        var talentInfo = LoadResData.inbronData;
        for (let index in talentInfo) {
            if (this.id == talentInfo[index]["id"]) {
                this.text1.string = talentInfo[index]["desc"];
            }
        }
        var count  = 0;
        var curWeaponLv:any = LocalStorage.getObj("weaponLv");
        var weaponData = LoadResData.weaponsData;
        for (let i in curWeaponLv){
           for(let j in weaponData){
               if(curWeaponLv[i]["id"]==weaponData[j]["id"]){
                    if(this.id == weaponData[j]['skill_type']){
                        count +=Number(curWeaponLv[i]['lv'])*Number(weaponData[j]['skill_base']);
                    }
               }
           }
        }
        this.text2.string = String(count);
        if(count<=0){
            this.activateBtn.interactable = false;
        }
    }
    public onStart() {
        this.bgClickable = true
        this.setCancelableOutSide(false);
        this.setClickListener(this.returnBtn, this.onReturnBtn);
        //this.setClickListener(this.activateBtn, this.onActivateBtn);
    }
    private onReturnBtn() {
        this.finish();
    }
    private onActivateBtn() {
        var state = 0;
        var list = [];
        this.state == 0 ? state = 1 : state = 0;
        var talentData: any = LocalStorage.getObj("talent");
        for (let index in talentData) {
            if (this.id == talentData[index]['id']) {
                var item = { 'id': talentData[index]['id'], "state": state };
                talentData.splice(Number(index), 1, item);
            }
           
        }
        LocalStorage.setObj("talent", talentData);
        Context.get().messenger.sendMessage(WxEvent.ON_CHANGETALENT, [this.id]);
        this.finish();
    }
    /*  public showTalent(title: string) {
         this.node.opacity = 0;
         this.text.string = title;
         this.node.runAction(cc.fadeIn(0.15));
         this.scheduleOnce(this.dismiss, 0.6)
     }
 
     public dismiss() {
         CommonUtil.runAction(this, cc.spawn(cc.fadeOut(0.4), cc.moveBy(0.4, 0, 100))).then(() => {
             this.finish();
         })
     } */
}